package com.zhihuichongcheng.application.ui.approval.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.smartlib.cmnObject.dialog.DialogOnClick;
import com.smartlib.cmnObject.util.DateUtil;
import com.smartlib.cmnObject.util.DialogUtil;
import com.smartlib.cmnObject.util.GsonUtil;
import com.smartlib.cmnObject.util.ToastOpt;
import com.yunkeshuzhi.common.utils.SSUtils;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;
import com.zhihuichongcheng.application.bean.approval.ApprovalChoiceBean;
import com.zhihuichongcheng.application.constant.NetworkAPI;
import com.zhihuichongcheng.application.net.HttpCallback;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by 3water on 2016/11/18.
 */

public class ApprovalSubmitActivity extends BaseActivity implements View.OnClickListener {
    private TextView mTvSubmit, mTvDate, mTvRemark;
    private RelativeLayout mRlDate;
    private EditText mEtComment;
    private String mUserFormInstanceId, mTaskId, mStrDate, mStrRemark;
    private LinearLayout mLayoutChoice;
    private List<ApprovalChoiceBean> mChoiceBeans = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_approval_submit);
        initView();
        initData();
        requestChoiceItem();
    }

    private void initView() {

        setTitle("同意审批");
        mTvSubmit = (TextView) findViewById(R.id.tv_submit);
        mRlDate = (RelativeLayout) findViewById(R.id.rl_date);
        mTvDate = (TextView) findViewById(R.id.tv_date);
        mEtComment = (EditText) findViewById(R.id.et_comment);
        mTvRemark = (TextView) findViewById(R.id.tv_remark);
        mLayoutChoice = findViewById(R.id.layout_choice_items);
        mTvSubmit.setOnClickListener(this);
        mRlDate.setOnClickListener(this);
    }

    private void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            if (intent.hasExtra("userFormInstanceId")) {
                mUserFormInstanceId = intent.getStringExtra("userFormInstanceId");
            }
            if (intent.hasExtra("taskId")) {
                mTaskId = intent.getStringExtra("taskId");
            }
            if (intent.hasExtra("remark")) {
                mStrRemark = intent.getStringExtra("remark");
            }
        }
        if (!SSUtils.isEmpty(mStrRemark))
            mTvRemark.setText("备注：" + mStrRemark);
        else
            mTvRemark.setVisibility(View.GONE);
        mStrDate = DateUtil.transferLongToDate("yyyy-MM-dd", System.currentTimeMillis());
        Date date = DateUtil.parseStringToDate(mStrDate, "yyyy-MM-dd");
        mTvDate.setText(DateUtil.formatDateToString(date, "yyyy年MM月dd日"));
    }

    private void requestChoiceItem() {
        final HashMap<String, String> param = new HashMap<>();
        param.put("taskId", mTaskId);
        mManager.post(NetworkAPI.USER_FORM_CHOICE, new HttpCallback() {
            @Override
            public void onSuccess(int code, String value, String msg) {
                mChoiceBeans = GsonUtil.paraeFromStringToList(value, ApprovalChoiceBean.class);
                if (code == 1) {
                    for (int i = 0; i < mChoiceBeans.size(); i++) {
                        final ApprovalChoiceBean itemBean = mChoiceBeans.get(i);
                        ViewGroup itemView = (ViewGroup) LayoutInflater.from(mLayoutChoice.getContext())
                                .inflate(R.layout.layout_approval_choice, mLayoutChoice, false);
                        TextView tvTitle = itemView.findViewById(R.id.tv_title);
                        tvTitle.setText(itemBean.getName());
                        switch (itemBean.getType()) {
                            case 0://单选
                                RadioGroup radioGroup = new RadioGroup(mLayoutChoice.getContext());
                                radioGroup.setOrientation(LinearLayout.HORIZONTAL);
                                for (int y = 0; y < itemBean.getParams().size(); y++) {
                                    ApprovalChoiceBean.ParamsBean paramsBean = itemBean.getParams().get(y);
                                    final RadioButton radioButton = new RadioButton(mLayoutChoice.getContext());
                                    radioButton.setText(paramsBean.getKey());
                                    radioGroup.addView(radioButton);
                                    final int finalY = y;
                                    radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                                        @Override
                                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                            for (int j = 0; j < itemBean.getParams().size(); j++) {
                                                itemBean.getParams().get(j).setSelected(j == finalY);
                                            }
                                        }
                                    });
                                }
                                itemView.addView(radioGroup);
                                break;
                            case 1://多选
                                CheckBox checkBox = new CheckBox(mLayoutChoice.getContext());
                                checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                                    @Override
                                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                        itemBean.setSelected(isChecked);
                                    }
                                });
                                itemView.addView(checkBox);
                                break;
                        }

                        mLayoutChoice.addView(itemView, 0);
                    }
                } else {
                    ToastOpt.createToast(mContext, msg);
                }
            }

            @Override
            public void onFailure(String value) {
            }
        }, param);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_submit:
                DialogUtil.showLoadDiadlog(mContext);
                HashMap<String, String> param = new HashMap<>();
                param.put("token", mUser.getToken());
                param.put("taskId", mTaskId);
                param.put("userFormInstanceId", mUserFormInstanceId);
                param.put("auditDate", mStrDate);
                param.put("remark", mEtComment.getText().toString());
                param.put("status", "2");//同意为2，拒绝3
                for (int i = 0; i < mChoiceBeans.size(); i++) {
                    ApprovalChoiceBean approvalChoiceBean = mChoiceBeans.get(i);
                    switch (approvalChoiceBean.getType()) {
                        case 0://单选
                            for (int j = 0; j < approvalChoiceBean.getParams().size(); j++) {
                                ApprovalChoiceBean.ParamsBean paramsBean = approvalChoiceBean.getParams().get(j);
                                if (paramsBean.isSelected())
                                    param.put(approvalChoiceBean.getField(), paramsBean.getValue());
                            }
                            break;
                        case 1://多选
                            if (approvalChoiceBean.isSelected())
                                param.put(approvalChoiceBean.getField(), "1");
                            break;
                    }
                }
                mManager.post(NetworkAPI.APproval_Deal, new HttpCallback() {
                    @Override
                    public void onSuccess(int code, String value, String msg) {
                        if (code == 1) {
                            ToastOpt.createToast(mContext, "审批成功");
                            setResult(Activity.RESULT_OK);
                            finish();
                        } else {
                            ToastOpt.createToast(mContext, msg);
                        }
                        DialogUtil.dismiss(mContext);
                    }

                    @Override
                    public void onFailure(String value) {
                        DialogUtil.dismiss(mContext);
                    }
                }, param);
                break;
            case R.id.rl_date:
                DialogUtil.showDatePick(mContext, new DialogOnClick<String>() {
                    @Override
                    public void onClick(String s) {
                        mStrDate = s;
                        Date date = DateUtil.parseStringToDate(s, "yyyy-MM-dd");
                        mTvDate.setText(DateUtil.formatDateToString(date, "yyyy年MM月dd日"));
                    }
                });
                break;
            default:
                break;
        }
    }
}
